//World Constructor
function World(display,message,stats) {
	this.Sections = new Object();
	this.Player;
	this.Display = new Display(display);
	this.Display.clear();
	this.Display.refresh();
	this.Message = new Message(message);
	this.Stats = new Stats(stats);
	this.Dice = new Dice();
	this.Skills = new Skills();
	this.AStar = new AStar()
	this.Menu = new Menu(this,message);
	this.Menu.mainMenu();
	this.Flags=0;
	this.Time=0;
	this.BMVTime = [0,0.4,0.37,0.34,0.32,0.3,0.28,0.26,0.24,0.22,0.2,0.19,0.18,0.16,0.15,0.14,0.13,0.12,0.11,0.1,0.1,0.09,0.08,0.08,0.07,0.07,0.06,0.06,0.05,0.05,0.05];
}

World.prototype.initNPCCreators = function() {
	this.NPC = [];
	this.NPC["Zombie"] = new Zombie();
}

World.prototype.newGame = function() {
	this.Player = new Player("@","FFFFFF","P");
	this.Sections["0,0,0"] = new Section("0,0,0",this);
	//this.Sections["0,0,0"].genCave(.4);
	this.Sections["0,0,0"].genForest(.1);
	this.Sections["0,0,0"].genForestCabin();
	var t = this.Sections["0,0,0"].placePlayer("P",Math.floor(Math.random()*23)+1,Math.floor(Math.random()*79)+1);
	while (!t) {
		t = this.Sections["0,0,0"].placePlayer("P",Math.floor(Math.random()*23)+1,Math.floor(Math.random()*79)+1);
	}
	this.Sections["0,0,0"].updateDisplay();
	this.Display.refresh("Map");
	this.Player.Attrb["FC"] = 30;
	this.Player.Attrb["$"] = this.Dice.roll(8,4,6,1);
	this.Player.Stats["FRE"] = this.Dice.roll(10,4,6,-5);;
	this.Player.updateStats();
	this.Flags = 3;
	this.Menu.attributeMenu();
}

World.prototype.Save = function() {
	var i;
	//Save World Header
	//***TODO***
	
	//Save World Sections
	for (i in this.Sections) {
		this.Sections[i].Save();
	}
	
	//Save World Footer
	//***TODO***
};

World.prototype.Load = function() {
	var i=0,NumSections=0;
	
	//Load World Header
	//***TODO***
	
	//Load World Sections
	for (i=0;i<NumSections;i++) {
		this.Sections[i] = new Section(i);
		this.Sections[i].Load();
	}
	
	//Load World Footer
	//***TODO***
};

World.prototype.genSection = function() {
	var newsectkey = this.nextSectionKey(this.Player.Section.Key,this.Player.exitDirection());
	
	//Quit early if the section already exists.
	if (!(typeof this.Sections[newsectkey] == "undefined")) return;
	//Create New Section
	this.Sections[newsectkey] = new Section(newsectkey,this);
	//Choose Section Type
	this.Sections[newsectkey].genForest(.1);
	
}

World.prototype.reverseDirection = function(dir) {
	if (dir=="N") return "S";
	if (dir=="S") return "N";
	if (dir=="E") return "W";
	if (dir=="W") return "E";
	return dir;
} 

World.prototype.nextSectionKey = function(sectkey,dir) {
	var sectionloc = sectkey.split(",");
	sectionloc[0] = parseInt(sectionloc[0]);
	sectionloc[1] = parseInt(sectionloc[1]);
	sectionloc[2] = parseInt(sectionloc[2]);
	
	if (dir=="N") {sectionloc[0]--;}
	if (dir=="S") {sectionloc[0]++;}
	if (dir=="E") {sectionloc[1]++;}
	if (dir=="W") {sectionloc[1]--;}
	
	return (sectionloc[0]+","+sectionloc[1]+","+sectionloc[2]);
}